/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.SearchBook.View;

import Deal.ItemsCreater.Items_Book;
import Deal.SalesView.Logic.SalesLogic;
import Deal.ViewController.ViewController;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LiuBole
 */
public class SearchJPanel extends javax.swing.JPanel {

    private ViewController viewController;
    private SalesLogic salesLogic;
    private DefaultTableModel bookTable = new DefaultTableModel(null, Items_Book.BOOK_COLUMN_NAMES);
//    boolean settleNow = false;
    /**
     * Creates new form SearchJPanel
     */
    public SearchJPanel(ViewController vc, SalesLogic sl) {
        viewController = vc;
        salesLogic = sl;
        initComponents();
        initData(); //数据初始化
    }

    private void initData() {  
        this.bookTable.setDataVector(viewController.searchOrTable(".*"+""+".*"), Items_Book.BOOK_COLUMN_NAMES);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        //图书类别设置
        Object[] bookCatalog = viewController.getBookCategory();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(bookCatalog));
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable(){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "综合", "ISBN", "title", "author", "publisher" }));
        jComboBox1.setSelectedIndex(0);
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jTextField1.setText("");
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        jTable1.setModel(bookTable);
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jButton2.setText("收藏图书");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);

        jButton4.setText("加入购物车");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "所有类别", "", "", "" }));
        jComboBox2.setSelectedIndex(0);
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    //获取选中条目对象。若没有选择则发出提示
    private Object[] getSelectedObject() {
        Object[] book = null;
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            book = new Object[jTable1.getColumnCount()]; //记录选中的书籍信息
            for (int i = 0; i < jTable1.getColumnCount(); i++) {
                book[i] = jTable1.getValueAt(row, i);
            }
        } else {
            JOptionPane.showMessageDialog(null, "请选择一个条目!");
        }
        return book;
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(getSelectedObject()!=null){
            Object book[] = getSelectedObject();
            if(viewController.addToCollection("name", book)){
                JOptionPane.showMessageDialog(null, "已添加到收藏夹!");
            } else {
                JOptionPane.showMessageDialog(null, "添加失败!");
            }
            
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        addToCarts();
    }//GEN-LAST:event_jButton4ActionPerformed

    private boolean addToCarts(){
        Object book[] = getSelectedObject();
        int num;
        if (book != null) {
            try {
                num = Integer.parseInt(JOptionPane.showInputDialog(this, "书籍数量", 1));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "输入错误！");
                return false;
            }
            if(num <= 0){
                JOptionPane.showMessageDialog(this, "输入错误！");
                return false;
            }
            
            if(salesLogic.addToCarts(book, num)){
                JOptionPane.showMessageDialog(this, "已添加到购物车!");
                return true;
            } else {
                JOptionPane.showMessageDialog(null, "添加失败!");
                return false;
            }
        }//end if
        return false;
    }
    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        // TODO add your handling code here:
        search();
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
        // TODO add your handling code here:
        search();
    }//GEN-LAST:event_jComboBox2ItemStateChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        search();
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void search(){
        String category = String.valueOf(jComboBox2.getSelectedItem());//类别
        String key = String.valueOf(jComboBox1.getSelectedItem());//关键字
        String keywords = jTextField1.getText();
        
        if(category.equals("所有类别") && key.equals("综合")){
            this.bookTable.setDataVector(viewController.searchOrTable(".*"+keywords+".*"), Items_Book.BOOK_COLUMN_NAMES);
        }
        else if(category.equals("所有类别")){
            this.bookTable.setDataVector(viewController.searchKeywords(key, ".*"+keywords+".*"), Items_Book.BOOK_COLUMN_NAMES);
        }
        else if(key.equals("综合")){
            this.bookTable.setDataVector(viewController.searchCategory(".*"+category+".*"), Items_Book.BOOK_COLUMN_NAMES);
        }
        else{
            this.bookTable.setDataVector(viewController.searchAndTable(category, key, ".*"+keywords+".*"), Items_Book.BOOK_COLUMN_NAMES);
        }
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
